"""
@description: Control the insertion operations to the database on events.
@author: Zzay
@create: 2022/07/11
"""
import time

# constants
CONFIG_FILE_PATH = "allow_insert_database.txt"
INTERVAL = 60

while True:
    # read the config file
    file = open(CONFIG_FILE_PATH, "r")
    contents = file.read()
    file.close()
    
    # get "is_allowed" attribute
    is_allowed = contents[11:12]
    
    if is_allowed == "0":
        # if currently not allowed to insert, wait until the next interval comes
        print("Status: Not allowed")
        for i in range(INTERVAL, 0, -1):
            print("Wait %d seconds..." % i)
            time.sleep(1)
        # update status "is_allowed"
        file = open(CONFIG_FILE_PATH, "w")
        file.write("is_allowed=1")
        file.close()
    elif is_allowed == "1":
        # if currently allowed to insert
        print("Status: Allowed")
        time.sleep(1)
    else:
        pass
